Unity3D Building Damage Tutorial- Part II - MeshManager Class
The file: MeshManager.cs Lets look at my scheme once more: We have to produce two classes. MeshManager will recieve orders from programmer. Than, it will communicare with Aliases (of verticle class) & Unity, make some changes, and than send these changes to Unity. MeshManager Class OK, so lets write some code. using UnityEngine; using System.Collections; using System.Collections.Generic; using RTS; public class MeshManager { private Mesh mesh; public Vector3 ParentPosition; public List Aliases; public int[] VerticleToAliasArray; public List Triangles; float MinimumHeight; float MaximumHeight; RTS is a namespace with many Classes & other things from game, not really important. Lets look at variables: *private Mesh mesh; We will have distinct MeshManager for every one mesh. In other words one MeshManager can ony work with one mesh. *public Vector3 ParentPosition; global position of mesh from meshFilter.transform.position *public List Aliases An List with Aliases, which are of Verticle Class. I decided to use List instead of standard array, becouse I want to make adding and removing aliases possible. With List, its much easier. *public int[] VerticleToAliasArray;. It should have been an List too, but for now it will be int array. It is somegow intresting. Lets Look at it in cube project. The index means number of verticle in mesh.verticle array, and int value mean number of Alias. For example, alias number 0 has 3 verticles (number 0, 15 and 22) *public List Triangles; Here i decided to change the uncomfortable format of mesh.triangles array, to more confortable vector3 List. The index is number of traingle, Vector3.x , y and z mean the numbers of verticles participating in triangle. Verticle numbers from mesh.verticles, NOT alias numbers! *float MinimiumHeight and MaximumHeight We will talk about this later. Constructor public MeshManager (ref MeshFilter meshFilter){//constructor Initialise(); ParentPosition = meshFilter.transform.position; mesh = meshFilter.mesh; ManageVerticles(mesh.vertices); ManageTriangles(mesh.triangles); CalcualateMinimumHeight(); } So, Constructor takes a MeshFilter reference (not sure if it has to be reference, but for now it works). First, the Initialise() function is called, but it just initialises Aliases and Triangles Lists. Nothing intrensing ParentPosition = meshFilter.transform.position; // here we set the ParentPosition, easy mesh = meshFilter.mesh; //and set mesh, obvious too Than, we have a ManageVerticles(mesh.vertices); function. Let's look closer to it. ManageVerticles Method private void ManageVerticles(Vector3[] verticles){ Dictionary> TempAliases = new Dictionary>(); int i = 0; foreach (Vector3 verticle in verticles){ if(TempAliases.ContainsKey(verticle)){ TempAliasesverticle.Add(i); }else{ TempAliasesverticle = new List(); TempAliasesverticle.Add(i); } i++; } VerticleToAliasArray = new inti; i = 0; foreach(var pair in TempAliases){ Aliases.Add (new Verticle(this, i, pair.Value, pair.Key)); ManageVerticleToAliasArray(i,pair.Value); i++; } } ManageVerticles has to things to do: 1)Produce Aliases List 2)Produce ManageVerticleToAliasArray list. Before we do Aliases, we have to find which verticles are in the same position. It is done here: Dictionary> TempAliases = new Dictionary>(); int i = 0; foreach (Vector3 verticle in verticles){ if(TempAliases.ContainsKey(verticle)){ TempAliasesverticle.Add(i); }else{ TempAliasesverticle = new List(); TempAliasesverticle.Add(i); } i++; } TempAliases is a temporary Dictionary. Yo have to realise, that now we now only verticle Position (Vector3) and number. Noting more. Dictionary is a clever collection that has Key-Value method for storing data. Here, i will use Vector3 position as Key, and List of ints as Value. Why? You see, I've made a foreach loop. first, it see if we arleady have a verticle of its position in our dictionary if(TempAliases.ContainsKey(verticle)){ If yes, i add its Index! (the i) to the List., if not, i've just make a new list with a key of Vector3 Position and add verticle index to it. After the loop is done , we have a nice dictionary of Aliases. For example for a key Vector3(0.5 , 0.5 , -0.5) we have value being list of 3 ints (3,6,8) This mean that in that coords there were 3 verticles of numbers 3, 6 and 8. Than there is VerticleToAliasArray initialisation (in fact it could be in other opsition) Inportant here is that we initialise it with lemght of i, i being number of verticles in mesh.verticles. foreach(var pair in TempAliases){ Aliases.Add (new Verticle(this, i, pair.Value, pair.Key)); ManageVerticleToAliasArray(i,pair.Value); i++; } Here we take each pair in TempAliases (in cube there are 8 of them, the number of corners) and using them make three distinct things. #new Verticle(this, i, pair.Value, pair.Key) We Make a new Alias (being of Verticle class) by starting a constructor. I will talk about this class later. #Aliases.Add ( ... ) Here we add this alias to the Aliases List. #ManageVerticleToAliasArray(i,pair.Value); Add some data to VerticleToAliasArray array. It is done in ManageVerticleToAliasArray function. Let's look at it. ManageVerticlesToAliasArray method private void ManageVerticleToAliasArray(int i, List list){ foreach(int k in list){ VerticleToAliasArrayk = i; } } Quite simple. The int i is number of Alias, and The List consists of verticle numbers from mesh.verticle. We just assign the verticle number its Alias number. ManageTriangles method private void ManageTriangles(int[] triangles){ for(int i=0; i 2 . Pass info about triangles to the Aliases. Aliases[VerticleToAliasArray(int)vect.x].AddTriangle(j,ChangeVerticleToAlias(vect)); Lets split this code. *VerticleToAliasArray(int)vect.x We have the number of verticle (vect.x). We have to get the Alias number, so we look at VerticleToAliasArray to see it. Casting is there, becouse index in array is of int type, and Vector3 variables are floats. *Aliases[VerticleToAliasArray(int)vect.x] we take the right alias, and *.AddTriangle(j,ChangeVerticleToAlias(vect); start an AddTriangle method there. It takes the number of triangle ( j ) and Vector3, the result of ChangeVerticleToAlias(vect). ChangeVerticleToArray is an simple function private Vector3 ChangeVerticleToAlias(Vector3 vect){ Vector3 x = new Vector3(VerticleToAliasArray(int)vect.x, VerticleToAliasArray(int)vect.y, VerticleToAliasArray(int)vect.z); return x; } It just gets Vector3 with verticles numbers, and translates it to Aliases numbers. Ok, now we have to look closer to Verticle Class. Category:Unity3D Building Damage Tutorial Category:Unity3D Category:C Sharp